package com.online.shopping.service.impl;

import com.online.shopping.pojo.TbItem;
import com.online.shopping.service.ItemSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ItemSearchServiceImpl implements ItemSearchService {

	@Autowired
	private SolrTemplate solrTemplate;

	@Override
	public Map findAll() {
		Map map = new HashMap();
		Query query = new SimpleQuery("*:*");

		ScoredPage<TbItem> page =solrTemplate.queryForPage("collection1", query, TbItem.class);
		map.put("rows", page.getContent());
		return map;
	}

	@Override
	public Map search(Map searchMap) {
		Map map = new HashMap();
		Query query = new SimpleQuery("*:*");

		// item_keywords是solr的业务字段
		Criteria criteria = new Criteria("item_keywords").is(searchMap.get("keywords"));
		query.addCriteria(criteria);

		ScoredPage<TbItem> page = solrTemplate.queryForPage("collection1", query, TbItem.class);
		map.put("rows", page.getContent());
		return map;
	}

	@Override
	public void importList(List list) {
		solrTemplate.saveBeans("collection1", list);
		solrTemplate.commit("collection1");
	}
}
